Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
"#### Add your code here ####"Task is to recognize a faces
Aligned Face Dataset from Pinterest
This dataset contains 10.770 images for 100 people. All images are taken from 'Pinterest' and aligned using dlib library.
import tensorflow
tensorflow.__version__
from google.colab import drive
drive.mount('/content/drive')
import os
os.chdir(path='/content/drive/My Drive/Colab Notebooks/CV_PROJECT2/')
# To list project files that are used and present in the folder
os.listdir()
from zipfile import ZipFile
with ZipFile(file='Aligned Face Dataset from Pinterest.zip',mode='r') as zf:
zf.extractall()
# To list project files that are used and present in the folder
os.listdir()
import numpy as np
import os
class IdentityMetadata():
def __init__(self, base, name, file):
# print(base, name, file)
# dataset base directory
self.base = base
# identity name
self.name = name
# image file name
self.file = file
def __repr__(self):
return self.image_path()
def image_path(self):
return os.path.join(self.base, self.name, self.file)
def load_metadata(path):
metadata = []
for i in os.listdir(path):
for f in os.listdir(os.path.join(path, i)):
# Check file extension. Allow only jpg/jpeg' files.
ext = os.path.splitext(f)[1]
if ext == '.jpg' or ext == '.jpeg':
metadata.append(IdentityMetadata(path, i, f))
return np.array(metadata)
# metadata = load_metadata('images')
metadata = load_metadata('PINS')
type(metadata)
metadata.shape
len(metadata)
print(metadata[8720].base)
print(metadata[8720].name)
print(metadata[8720].file)
metadata[8720]
import cv2
def load_image(path):
img = cv2.imread(path, 1)
# OpenCV loads images with color channels
# in BGR order. So we need to reverse them
return img[...,::-1]
import matplotlib.pyplot as plt
img=load_image(metadata[8720].image_path())
plt.figure(figsize=(8,8))
plt.imshow(img)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import ZeroPadding2D, Convolution2D, MaxPooling2D, Dropout, Flatten, Activation
def vgg_face():
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))
return model
model = vgg_face()
model.load_weights(filepath='/content/drive/My Drive/Colab Notebooks/CV_PROJECT2/vgg_face_weights.h5')
model.summary()
from tensorflow.keras.models import Model
vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)
# Get embedding vector for first image in the metadata using the pre-trained model
img_path = metadata[8720].image_path()
img = load_image(img_path)
# Normalising pixel values from [0-255] to [0-1]: scale RGB values to interval [0,1]
img = (img / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
print(img.shape)
# Obtain embedding vector for an image
# Get the embedding vector for the above image using vgg_face_descriptor model and print the shape
embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
print(embedding_vector.shape)
Write code to iterate through metadata and create embeddings for each image using vgg_face_descriptor.predict() and store in a list with name embeddings
If there is any error in reading any image in the dataset, fill the emebdding vector of that image with 2622-zeroes as the final embedding from the model is of length 2622.
from tqdm import tqdm
embeddings = np.zeros((metadata.shape[0], 2622))
for i, m in tqdm(enumerate(metadata)):
img_path = metadata[i].image_path()
img = load_image(img_path)
# Normalising pixel values from [0-255] to [0-1]: scale RGB values to interval [0,1]
img = (img / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
embeddings[i]=embedding_vector
#Saving the embeddings into npy file so can re-use it without the need of running the predict func
np.save(file='/content/drive/My Drive/Colab Notebooks/CV_PROJECT2/embeddings.npy', arr=embeddings, allow_pickle=True)
def distance(emb1, emb2):
return np.sum(np.square(emb1 - emb2))
# Loading the embeddings that are saved
embeddings=np.load(file='/content/drive/My Drive/Colab Notebooks/CV_PROJECT2/embeddings.npy',allow_pickle=True)
embeddings.shape
import matplotlib.pyplot as plt
def show_pair(idx1, idx2):
plt.figure(figsize=(8,3))
plt.suptitle(f'Distance = {distance(embeddings[idx1], embeddings[idx2]):.2f}')
plt.subplot(121)
plt.imshow(load_image(metadata[idx1].image_path()))
plt.subplot(122)
plt.imshow(load_image(metadata[idx2].image_path()));
show_pair(8720, 3)
show_pair(8720, 8713)
show_pair(8720, 180)
train_idx = np.arange(metadata.shape[0]) % 9 != 0 #every 9th example goes in test data and rest go in train data
test_idx = np.arange(metadata.shape[0]) % 9 == 0
# train and test split
X_train = embeddings[train_idx]
X_test = embeddings[test_idx]
targets = np.array([m.name for m in metadata])
#train labels
y_train = targets[train_idx]
#test labels
y_test = targets[test_idx]
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
label_encoder = label_encoder.fit(y_train)
y_train_enc=label_encoder.transform(y_train)
y_test_enc=label_encoder.transform(y_test)
# Standarize features
from sklearn.preprocessing import StandardScaler
std_scale=StandardScaler()
std_scale= std_scale.fit(X_train)
X_train_scaled= std_scale.transform(X_train)
X_test_scaled= std_scale.transform(X_test)
from sklearn.decomposition import PCA
pca=PCA(n_components=128)
pca=pca.fit(X_train_scaled)
#X_pca_scaled=pca.transform(X_scaled)
print("Explained Variance ratios of each PCA: ",pca.explained_variance_ratio_)
print("Cummulative sum of Variance ratios for PCAs: ",np.cumsum(pca.explained_variance_ratio_))
# Obtaining PCA attributes for train and test set
X_train_pca_scaled=pca.transform(X_train_scaled)
X_test_pca_scaled=pca.transform(X_test_scaled)
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
svc_model=SVC()
svc_model=svc_model.fit(X_train_pca_scaled,y_train_enc)
y_train_pred=svc_model.predict(X_train_pca_scaled)
y_test_pred=svc_model.predict(X_test_pca_scaled)
print("Training data accuracy: ",accuracy_score(y_train_enc,y_train_pred))
print("Testing data accuracy: ",accuracy_score(y_test_enc,y_test_pred))
import warnings
# Suppress LabelEncoder warning
warnings.filterwarnings('ignore')
example_idx = 10
example_image = load_image(metadata[test_idx][example_idx].image_path())
example_prediction = y_test_pred[example_idx]
example_identity = label_encoder.inverse_transform(np.expand_dims(example_prediction,axis=0))[0][5:]
example_orig_identity = metadata[test_idx][example_idx].name[5:]
plt.imshow(example_image)
plt.title(f'Identified as {example_identity} by model and orginally {example_orig_identity}');
import warnings
# Suppress LabelEncoder warning
#warnings.filterwarnings('ignore')
for i in np.where(y_test=='pins_gal gadot face')[0]:
example_idx = i
example_image = load_image(metadata[test_idx][example_idx].image_path())
example_prediction = y_test_pred[example_idx]
example_identity = label_encoder.inverse_transform(np.expand_dims(example_prediction,axis=0))[0][5:]
example_orig_identity = metadata[test_idx][example_idx].name[5:]
plt.figure(figsize=(8,8))
plt.imshow(example_image)
plt.title(f'Identified as {example_identity} by model and orginally {example_orig_identity}');